from PyQt6.QtWidgets import  QWidget,QLabel,QHBoxLayout,QPushButton,QTreeWidget,QSplitter,QTreeWidgetItem,QMenu
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QFont
from db.DbmsDao import DbmsDao
from dbms.MysqlDao import MysqlDao
from utils.Logger import LogPrint
from dialog import ModifyDbmsDialog
from views.DbmsRightTabView import DbmsRightTabView

class DbmsOperateView(QWidget):
    def __init__(self, parent=None,date=None):
        super(DbmsOperateView, self).__init__(parent)
        # 初始化变量

        self.dict_db_instance={}
        self.dict_db_instance_treeitem={}

        self.init_ui()

    def init_ui(self):

        layout_all=QHBoxLayout()

        self.splitter = QSplitter(Qt.Orientation.Horizontal)
        self.splitter.setChildrenCollapsible(False) 
        self.view_db_list=self.create_db_treeview()
        self.splitter.addWidget(self.view_db_list)
        self.current_view=QLabel("None")
        self.splitter.addWidget(self.current_view)
        
        self.splitter.setStretchFactor(0,3)
        self.splitter.setStretchFactor(1,7)

        layout_all.addWidget(self.splitter)

        self.setLayout(layout_all)

    def create_db_treeview(self):

        self.db_treeview=QTreeWidget()
        self.db_treeview.setMaximumWidth(200)
        self.db_treeview.setHeaderHidden(True)

        dbms_list=DbmsDao.get_db_info()
        
        if dbms_list and len(dbms_list)>0:

            
            # 添加顶层项
            top_level_item = QTreeWidgetItem(self.db_treeview)
            top_level_item.setText(0, "DBMS")
            top_level_item.setFont(0, QFont('Arial', 12))  # 设置字体和大小
            #top_level_item.setBackground(0, QBrush(QColor(200, 150, 255)))  # 设置背景色
            top_level_item.setExpanded(True)

            for row in range(len(dbms_list)):

    
                # 添加--name
                child_item_dbms_name = QTreeWidgetItem()
                child_item_dbms_name.setText(0, dbms_list[row][1])
                child_item_dbms_name.setFont(0, QFont('Courier', 10))
                #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
                top_level_item.addChild(child_item_dbms_name)

                #读取数据库列表
                mysql_obj=MysqlDao(dbms_list[row][3],dbms_list[row][4],dbms_list[row][5],dbms_list[row][6])
                db_list=mysql_obj.get_all_databases()

                self.dict_db_instance[dbms_list[row][1]]=mysql_obj
                if db_list and len(db_list)>0:
                    for index in range(len(db_list)):
                    # 添加数据库名称
                        child_item_db_name = QTreeWidgetItem()
                        child_item_db_name.setText(0, db_list[index][0])
                        child_item_db_name.setFont(0, QFont('Courier', 10))
                        #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
                        child_item_dbms_name.addChild(child_item_db_name)
                        key=dbms_list[row][1]+'-'+db_list[index][0]
                        self.dict_db_instance_treeitem[key]=child_item_db_name

        self.db_treeview.itemClicked.connect(self.on_item_clicked)

        return self.db_treeview
            
    def on_item_clicked(self, item:QTreeWidgetItem):

        if item.parent() is None:   
            return


        # 这里处理点击事件
        print(f"Item clicked: {item.text(0)},{item.parent().text(0)}") 
        if item.parent().text(0):
            if  item.parent().text(0) in self.dict_db_instance:
                mysql_obj=self.dict_db_instance[item.parent().text(0)]
                table_list=mysql_obj.get_all_tables(item.text(0))
                if table_list and len(table_list)>0:
                        for index in range(len(table_list)):
                        # 添加数据库名称
                            child_item_table_name = QTreeWidgetItem()
                            child_item_table_name.setText(0, table_list[index][0])
                            child_item_table_name.setFont(0, QFont('Courier', 10))
                            #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))

                            key=item.parent().text(0)+'-'+item.text(0)
                            child_item_db_name=self.dict_db_instance_treeitem[key]
                            child_item_db_name.addChild(child_item_table_name)
            elif item.parent().text(0)=='DBMS':
                #点击二级item
                pass
            else:
                #点击表
                table_name=item.text(0)
                db_name=item.parent().text(0)
                mysql_obj=self.dict_db_instance[item.parent().parent().text(0)]

                self.current_view=DbmsRightTabView(self,mysql_obj,db_name,table_name)
                self.splitter.replaceWidget(1,self.current_view)
